package com.s69.gyh.mybatis_plus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.s69.gyh.mybatis_plus.entity.Book;
import com.s69.gyh.mybatis_plus.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/books")
public class BookController {
    @Autowired
    IBookService bookService;
    @GetMapping
    public String books(Model model){
        List<Book> list = bookService.list();
        model.addAttribute("list",list);
        return "books";
    }

    @GetMapping("/page")
    public String page(Model model,@RequestParam(defaultValue = "1") Integer pageNum){
        IPage page = bookService.getPage(pageNum);
        model.addAttribute("page",page);
        return "bookPage";
    }

    @PostMapping
    public String searchBook(Model model,Book book){
        List<Book> list=bookService.searchBook(book);
        model.addAttribute("list",list);
        model.addAttribute("bookname", book.getName());
        model.addAttribute("author", book.getAuthor());
        model.addAttribute("category",book.getCategory());
        return "books";
    }
}
